<template>
	<view>
		<u-form-item :key="item.key" :label="$t(item.label)" :required="item.required">
			<u-select v-model="show" :default-value="defaultValue" :confirm-color="confirmColor" :list="radioList[type]"
				mode="single-column" @confirm="confirm">
			</u-select>
			<u-input v-model="cityText" :placeholder="$t(item.placeholder)" disabled @click="show = true"
				:custom-style="myStyle" :placeholder-style="placeholderStyle" />
		</u-form-item>
	</view>
</template>
<script>
	import {
		COUNTRY
	} from '@/config/app';

	export default {
		props: {
			item: {
				type: Object,
				default: {},
				required: true,
			}
		},
		computed: {},
		data() {
			return {
				myStyle: {
				  textAlign: 'left',
						pointerEvents: "none"
				},
				placeholderStyle: "text-align: left;",
				radioList: {
					default: [ // 默认 是否单选
						{
							label: '否',
							value: '1',
							checked: false
						},
						{
							label: '是',
							value: '2',
							checked: false
						}
					],
					marginType: [ // 金额单位
						{
							label: '元',
							value: '1',
							checked: true
						},
						{
							label: '卢布',
							value: '2',
							checked: false
						}
					],
					priceUnit: [ // 价格单位
						{
							label: '元',
							value: '元',
							checked: true
						},
						{
							label: '卢布',
							value: '卢布',
							checked: false
						}
					],
					speedUnit: [ // 速度单位
						{
							label: '天',
							value: '天',
							checked: true
						},
					],
					numberUnit: []
				},

				labelStyle: {
					'word-break': 'break-all',
				},
				lang: getApp().globalData.lang,
				show: false,
				cityText: '',
				country: "0",
				list: COUNTRY,
				confirmColor: '#2979ff',
				defaultValue: [],
				searchText: '',
				type: 'default',
				// countryList: COUNTRY,
				// provinceList: [],
				// cityList: [],
			};
		},
		async mounted() {
			await this.getList()
			if (this.item.details && this.item.details.type) {
				this.type = this.item.details.type
			}
			this.setDefaultValue()
		},
		methods: {
			setDefaultValue() { // 设置默认值
				let radioList = this.radioList[this.type]
				for (let i = 0; i < radioList.length; i++) {
					if (this.item.value === radioList[i].value) {
						this.defaultValue = [radioList[i].value]
						this.cityText = radioList[i].label
					}
				}
			},
			districts(val) {
				return val.length > 10 ? val.slice(0, 23) + "...>" : val
			},
			async getList() {
				uni.showLoading({
					title: "loading...",
					mask: true
				})
				const that = this
				const data = await this.$u.api.basic.getUnitList()
				uni.hideLoading()
				const formatData = data.map(item => {
					return {
						label: item[this.$t('item.name')],
						value: item[this.$t('item.name')],
						checked: false
					}
				})
				this.$set(this.radioList,'numberUnit',formatData)
			},
			confirm(e) {
				this.cityText = ''
				for (let i = 0; i < e.length; i++) {
					let str = '';
					if (i > 0) {
						str = ','
					}
					this.cityText += str + e[i].label
				}
				this.$emit('change', this.item.key, e[0].value)
			},
			mapDetails(res) {
				// 默认显示，取各级对应下标
				let value = this.item.value
				let country = value.country
				let province = value.province
				let city = value.city
				let value1 = 0 // 国家下标
				let value2 = 0 // 省下标
				let value3 = 0 // 市下标
				if (country === "1") {
					value1 = 1
				}
				let children1 = res[value1].children
				let children2 = []
				for (let i in children1) {
					if (children1[i].value === province) {
						value2 = parseInt(i)
						children2 = children1[i].children
					}
				}
				for (let j in children2) {
					if (children2[j].value === city) {
						value3 = parseInt(j)
					}
				}
				this.defaultValue = [value1, value2, value3]
				const lang = uni.getStorageSync('lang')
				if (lang === 'ru') {
					this.cityText = value.mergename_ru
				} else {
					this.cityText = value.mergename
				}
			}
		}
	}
</script>

<style lang="scss">

</style>